﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.Adapters;

namespace Active.Controls.Adapters
{
    /// <summary>
    /// ActiveAdapter, 专门用于积极广告系统的广告周期的CheckBoxList
    /// 已直接继承CheckBoxList, 放弃这种方式了.
    /// </summary>
    public class ActiveCheckBoxListAdapter : WebControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            ListControl listControl = this.Control as ListControl;
            IRepeatInfoUser repeatInfo = this.Control as IRepeatInfoUser;

            if (listControl.CssClass != "campaignPeriod" || listControl == null || repeatInfo == null)
            {
                base.Render(writer);
                return;
            }

            if (listControl.CssClass.Length > 0)
                writer.AddAttribute("class", listControl.CssClass);

            writer.AddAttribute("cellspacing", "0");  //这两个一定要加, 晕死....
            writer.AddAttribute("cellpadding", "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            if (listControl.Attributes["Month1"].Length > 0)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddAttribute("colspan", listControl.Attributes["Month1Count"]);
                writer.RenderBeginTag(HtmlTextWriterTag.Th);
                writer.Write(listControl.Attributes["Month1"]);
                writer.RenderEndTag();

                if (listControl.Attributes["Month2"].Length > 0)
                {
                    writer.AddAttribute("colspan", listControl.Attributes["Month2Count"]);
                    writer.RenderBeginTag(HtmlTextWriterTag.Th);
                    writer.Write(listControl.Attributes["Month2"]);
                    writer.RenderEndTag();
                }

                writer.RenderEndTag();
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            foreach (ListItem item in listControl.Items)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(item.Value);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            for (int i = 0; i < listControl.Items.Count; i++)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                listControl.Items[i].Text = "";
                repeatInfo.RenderItem(ListItemType.Item, i, new RepeatInfo(), writer);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();

            writer.RenderEndTag();
        }

    }
}
